package com.opengroupe.dao.impl;

// Generated 31 juil. 2012 15:44:44 by Hibernate Tools 3.4.0.CR1

import java.util.List;

import javax.ejb.Stateless;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.orm.jpa.support.JpaDaoSupport;

import com.opengroupe.dao.CandidatDao;
import com.opengroupe.entities.Candidat;

/**
 * Home object for domain model class Candidat.
 * @see com.opengroupe.entities.Candidat
 * @author Hibernate Tools
 */
@Stateless
public class CandidatDaoImpl extends JpaDaoSupport implements CandidatDao {

	private static final Log log = LogFactory.getLog(CandidatDaoImpl.class);

	public Candidat persist(Candidat transientInstance) throws DataAccessException{
		log.debug("persisting Candidat instance");
		try {
			getJpaTemplate().persist(transientInstance);
			log.debug("persist successful");
			return transientInstance;
		} catch (DataAccessException re) {
			log.error("persist failed", re);
			throw re;
		}
	}

	public void remove(Candidat persistentInstance) {
		log.debug("removing Candidat instance");
		try {
			getJpaTemplate().remove(persistentInstance);
			log.debug("remove successful");
		} catch (RuntimeException re) {
			log.error("remove failed", re);
			throw re;
		}
	}

	public Candidat merge(Candidat detachedInstance) {
		log.debug("merging Candidat instance");
		try {
			Candidat result = getJpaTemplate().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public Candidat findById(Integer id) {
		log.debug("getting Candidat instance with id: " + id);
		try {
			Candidat instance = getJpaTemplate().find(Candidat.class, id);
			log.debug("get successful");
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public Candidat findByEmail(String email) {
		try {
			Candidat candidat = (Candidat) getJpaTemplate().findByNamedQuery("findByEmail", email).get(0);
			return candidat;
		} catch (RuntimeException  e) {
			log.error("get by email failed", e);
			throw e;
		}
	}

	@SuppressWarnings("unchecked")
	public List<Candidat> findByNomPrenom(String nom, String prenom) {
		log.debug("getting Candidat instance with nom: " + nom + " and prenom : "+prenom);
		try {
			List<Candidat> candidats = (List<Candidat>)getJpaTemplate().findByNamedQuery("findByNomPrenom", nom, prenom);
			return candidats;
		} catch (RuntimeException e) {
			log.error("get by nom and prenom failed", e);
		}
		return null;
	}

}
